<?php

namespace mpend\modules\team\models;

use Yii;
use common\logics\WorkbookLogic;
use common\models\workbook\WorkbookMember;
use common\helpers\ArrayHelper;
use common\helpers\Url;

/**
 * 班组记工本管理模型
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class TeamMemberSearch extends WorkbookMember {

    /**
     * @inheritdoc
     */
    public function buildQuery($alias = 't') {
        $query = static::find();
        if ($alias) {
            $query->alias($alias);
            $alias .= '.';
        }
        $query->active(static::STATUS_ACTIVE);
        $query->joinWith([
            'workbook' => function($q) {
                return $q->alias('workbook')->active(WorkbookLogic::STATUS_ACTIVE);
            }
        ]);
        $query->andWhere([
            $alias . 'workbook_id' => $this->workbook_id,
            'workbook.type' => WorkbookLogic::TYPE_TEAM,
        ]);
        $query->orderBy([
            $alias . 'created_at' => SORT_ASC,
        ]);
        $query->debug();
        return $query;
    }

    /**
     * @return string
     */
    public function getDescription() {
        return implode(' ', [
            $this->occupation,
            $this->dailyWage,
        ]);
    }

    /**
     * 记工本所有操作类型
     * @return array
     */
    public function getConsoles() {
        return [
            'update' => [
                'name' => '编辑工人信息',
                'link' => [
                    'type' => 'push',
                    'route' => '/pages/form/worker/main',
                    'params' => [
                        'id' => $this->id
                    ],
                ],
            ],
            'bind' => [
                'name' => '绑定微信',
                'link' => [
                    'type' => 'push',
                    'route' => '/pages/worker/main',
                    'params' => [
                        'id' => $this->id
                    ],
                ],
            ],
            'unbind' => [
                'name' => '解除微信绑定',
                'confirm' => [
                    'message' => '解绑后，该工人将无法查看当前记工簿的记工信息，是否继续操作？',
                    'request' => [
                        'method' => 'post',
                        'params' => [
                            'type' => 'unbind'
                        ],
                        'url' => Url::to(['/team/member/update', 'id' => $this->id], true)
                    ],
                ],
            ],
            'as_master' => [
                'name' => '设为班组长',
                'confirm' => [
                    'message' => '每个记工薄只能设置一个班组长，如已设置了其他管理人员为班组长将替换当前管理员为班组长，是否继续操作？',
                    'request' => [
                        'method' => 'post',
                        'params' => [
                            'type' => 'as_master'
                        ],
                        'url' => Url::to(['/team/member/update', 'id' => $this->id], true)
                    ],
                ],
            ],
            'as_manager' => [
                'name' => '设置为管理员',
                'confirm' => [
                    'message' => '是否设置当前工人为管理员？',
                    'request' => [
                        'method' => 'post',
                        'params' => [
                            'type' => 'as_manager'
                        ],
                        'url' => Url::to(['/team/member/update', 'id' => $this->id], true)
                    ],
                ],
            ],
            'release_master' => [
                'name' => '解除班组长',
                'confirm' => [
                    'message' => '解除班组长后，该记工簿记工权限将只有创建者拥有，班组长将自动降级为管理员，是否继续操作？',
                    'request' => [
                        'method' => 'post',
                        'params' => [
                            'type' => 'release_master'
                        ],
                        'url' => Url::to(['/team/member/update', 'id' => $this->id], true)
                    ],
                ],
            ],
            'release_manager' => [
                'name' => '取消管理员',
                'confirm' => [
                    'message' => '取消后，该管理员将无法管理该记工簿，是否继续操作？',
                    'request' => [
                        'method' => 'post',
                        'params' => [
                            'type' => 'release_manager'
                        ],
                        'url' => Url::to(['/team/member/update', 'id' => $this->id], true)
                    ],
                ],
            ],
            'delete' => [
                'name' => '删除工人',
                'confirm' => [
                    'message' => '删除后，该工人将从记工薄中移除，是否继续操作？',
                    'request' => [
                        'method' => 'post',
                        'params' => [],
                        'url' => Url::to(['/team/member/delete', 'id' => $this->id], true)
                    ],
                ],
            ],
        ];
    }

    /**
     * @return string
     */
    public function getActions() {
        //update,bind,unbind,as_master,as_manager,release_master,release_manager,delete
        $consoles = $this->consoles;
        if ($this->user_id) {
            ArrayHelper::remove($consoles, 'bind');
            if ($this->role_id == static::ROLE_SUPER) {
                $consoles = ArrayHelper::filter($consoles, [
                    'update',
                    'release_master',
                ]);
                if ($this->workbook->user_id !== Yii::$app->user->id) {
                    ArrayHelper::remove($consoles, 'release_master');
                }
            } elseif ($this->role_id == static::ROLE_ADMIN) {
                $consoles = ArrayHelper::filter($consoles, [
                    'update',
                    'as_master',
                    'release_manager',
                ]);
                if ($this->workbook->user_id !== Yii::$app->user->id) {
                    ArrayHelper::remove($consoles, 'as_master');
                }
            } else {
                $consoles = ArrayHelper::filter($consoles, [
                    'update',
                    'unbind',
                    'as_manager',
                    'delete',
                ]);
            }
        } else {
            $consoles = ArrayHelper::filter($consoles, [
                'update',
                'bind',
                'delete',
            ]);
        }

        return array_values($consoles);
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id',
            'realname',
            'occupation',
            'avatar',
            'workbook_id',
            'user_id',
            'role_id',
            'abbrs',
            'wage',
            'dailyWage',
            'description',
            'managed',
            'actions',
        ];
    }

}
